package cn.doit.crawler.demo

import java.io.InputStream
import java.util

import org.apache.commons.io.IOUtils
import org.apache.http.Header
import org.apache.http.client.methods.{CloseableHttpResponse, HttpGet}
import org.apache.http.impl.client.{CloseableHttpClient, HttpClientBuilder}
import org.apache.http.message.BasicHeader

object HttpClientDemo {
  def main(args: Array[String]): Unit = {
    // 1、设置头文件
    val h1 = new BasicHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
    val h2 = new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0")
    val headers = new util.ArrayList[Header]()
    headers.add(h1)
    headers.add(h2)
    // 2、创建客户端
    val client  = HttpClientBuilder.create().setDefaultHeaders(headers).build()
    // 3、设置httpget
    val url = "http://www.appchina.com/"
    val get = new HttpGet(url)
    // 4、客户端发送get请求
    val response: CloseableHttpResponse = client.execute(get)
    // 5、获取响应内容
    val content: InputStream = response.getEntity.getContent
    // 6、使用IOUtils读取内容
    val strings: util.List[String] = IOUtils.readLines(content)
    import scala.collection.JavaConversions._
    strings.foreach(println)
    // 7、关闭客户端
    client.close()
  }
}
